	var calendars = new Array();

	// JavaScript funcions required by the Calendar class
	function timelink(i, n_ampm) {
		var out = "<a href=\"#\" onClick=\"setWhen(trim(_('event_when_text').value+' " + i + "'+ampm)); hide('timechooser');return false;\">" + i;
		if (n_ampm) out += "<span id='ampm_" + n_ampm + "'>pm</span>";
		out+="</a>";
		return out;
	}

	function doAmPm(ampm) {
		for(i=1; i<=12; i++)	{
			_('ampm_'+i).innerHTML=ampm;
		}
		id = 'button_'+ampm;
		if (ampm=='am') {
			_('button_am').className='ampm_on';
			_('button_pm').className='ampm_off';
		} else {
			_('button_am').className='ampm_off';
			_('button_pm').className='ampm_on';
		}
	}
	function Calendar(id, lyr, lmth, lday) {
		this.id = id;
		calendars[id] = this;
		if (!lmth) this.ltext = lyr; else {this.lyr = lyr; this.lmth = lmth; this.lday = lday;}
		
		if (!lmth) {
			$out = 
				"<img id='cal_ctrl_icon_"+id+"' onclick=\"this.cal.expand(); return false;\"  src='"+calendar_icon+"' " +
						"style='vertical-align:middle;'/>" +
				"<img src='"+calendar_time_icon+"' onclick=\"show('timechooser');return false;\" alt='pick time'" +
						"style='vertical-align:middle;'/>" +
				"<a href='#' onClick=\"setWhen(''); _('event_when_text').focus();return false;\">[X]</a>" +
				"<div id='timechooser' class='timechooser' style='display: none; position: absolute; width: 420px;padding:5px'>" +
					"<div class='controls' align='center'>" +
						"<div style='float: right;'><a href='#' onClick='hide(\"timechooser\");return false;'>[X]</a></div>";
			$out += "<a href='#' onClick=\"ampm='am';doAmPm(ampm);return false;\" id=\"button_am\">am</a>&nbsp;|&nbsp;" +
						"<a href='#' onClick=\"ampm='pm';doAmPm(ampm);return false;\" id=\"button_pm\">pm</a></div>" +
						"<table><tr>";
			for (var i=0; i<12; i++) $out += "<th>" + timelink((i?i:12), (i?i:12)) + "</td>";
			$out += "</tr><tr>";
			for (var j=1; j<4; j++) { var min = j*15;
				$out += "<tr class='smaller'>";
				for (var i=0; i<12; i++) $out += "<td>" + timelink((i?i:12)+':'+min, false) + "</td>";
				$out += "</tr>";
			}
			$out += "</tr></table></div>";
		} else
		$out = 
			"<a id='cal_ctrl_show_"+id+"' href='#' onclick='this.cal.expand();return false;' class='calendar_show'></a>" +
			"<a id='cal_ctrl_add_"+id+"' href='#' onClick='this.cal.init(1);this.cal.expand();return false;' class='calendar_show'>Add date</a>" +
			"<!--<a id='cal_ctrl_del_"+id+"' href='#' class='cal_ctrl_del' onClick='this.cal.init(-1);return false;'>delete date</a>-->";
		$out +=
			"<div style='position:absolute;'><div id='cal_ctrl_"+id+"' class='calendar'>" +
				"<div class='topbox'>" +
					"<div class='chosendate' id='chosendate"+id+"'></div>"+
					"&nbsp;&nbsp;<a href='#' class='todaylink' onClick=\"parentNode.parentNode.cal.today(); return false;\">today</a>" +
				"</div>"+
				"<div class='month_selector'>" +
				  "<select name='dropdown_month"+id+"' id='dropdown_month"+id+"' onChange='parentNode.parentNode.cal.draw();' class='dropdowntext'>" +
					"<option value='1'>January</option><option value='2'>February</option><option value='3'>March</option>" +
					"<option value='4'>April</option><option value='5'>May</option><option value='6'>June</option>" +
					"<option value='7'>July</option><option value='8'>August</option><option value='9'>September</option>" +
					"<option value='10'>October</option><option value='11'>November</option><option value='12'>December</option>" +
				  "</select>" +
				"<input class='yearselector' size='4' id='yearselector"+id+"' onkeypress='return false;'/>" +
				"<a href='#' style='font-size:6pt' onClick=\"_('yearselector"+id+"').value=-1+parseInt(_('yearselector"+id+"').value); parentNode.parentNode.cal.draw();return false;\">" +
					"<img src='"+calendar_arrow_left+"' width='12' height='12' border='0' alt='<'></a>" +
				"<a href='#' style='font-size:6pt;' onClick=\"_('yearselector"+id+"').value=1+parseInt(_('yearselector"+id+"').value); parentNode.parentNode.cal.draw();return false;\">" +
				"<img src='"+calendar_arrow_right+"' width='12' height='12' border='0' alt='>'></a>" +
				"</div>"+
				"<div class='daysholder'><div class='dh'>S</div><div class='dh'>M</div><div class='dh'>T</div><div class='dh'>W</div><div class='dh'>T</div><div class='dh'>F</div><div class='dh'>S</div>";
		for (var i=1; i<=42; i++) $out+="<div id='"+i+id+"' class='ds'>"+i+"</div>";
		$out +=
				"</div>" +
				"<div class='calendar_buttons'>" +
					"<input type='button' value='Select' onClick='var cal=parentNode.parentNode.cal;cal.collapse(); cal.save();'>" +
				"<input type='button' value='Cancel' onClick='var cal=parentNode.parentNode.cal;cal.collapse();'>" +
				"</div>"+
			"</div></div>";
		document.write($out);
		ampm='pm';
		if (!lmth) doAmPm(ampm);
		this.cal = _("cal_ctrl_"+this.id);
		this.calicon = _("cal_ctrl_icon_"+this.id);
		this.calshow = _("cal_ctrl_show_"+this.id);
		this.caladd = _("cal_ctrl_add_"+this.id);
		this.caldel = _("cal_ctrl_del_"+this.id);
		this.caldate = _('chosendate'+this.id);
		this.yearselector = _('yearselector'+this.id);
		this.dropdown_month = _('dropdown_month'+this.id);
		this.cal.cal = this;
		if (this.calicon) this.calicon.cal = this;
		if (this.calshow) this.calshow.cal = this;
		if (this.caladd) this.caladd.cal = this;
		if (this.caldel) this.caldel.cal = this;
		this.init();
	}
	function cal_show() { 
		show("cal_ctrl_show_"+this.id, 'inline'); 
		show("cal_ctrl_del_"+this.id, 'inline'); 
		hide("cal_ctrl_add_"+this.id); 
	}
	Calendar.prototype.show = cal_show;
	function cal_hide() { 
		hide("cal_ctrl_show_"+this.id); 
		hide("cal_ctrl_del_"+this.id); 
		show("cal_ctrl_add_"+this.id, 'inline'); 
	}
	Calendar.prototype.hide = cal_hide;
	function cal_expand() { show("cal_ctrl_"+this.id, 'block'); }
	Calendar.prototype.expand = cal_expand;
	function cal_collapse() { hide("cal_ctrl_"+this.id); }
	Calendar.prototype.collapse = cal_collapse;
	function cal_init(when) { 
		if (!when) when=0;
		var date = new Date();
		var chg = true;
		if (when<1) {
			chg=false;
			if(this.lyr && this.lyr.value>0) {date.setYear(this.lyr.value);chg=true;}
			if(this.lmth && this.lmth.value>0) {date.setMonth(this.lmth.value-1);chg=true;}
			if(this.lday && this.lday.value>0) {date.setDate(this.lday.value);chg=true;}
			if (this.ltext) chg=true;
		}
		if (when<0 || !chg) {
			if (this.ltext)  this.ltext.value = '';
			else {
				this.lday.value = 0;
				this.lmth.value = 0;
				this.lyr.value = 0;
			}
			this.hide();
			this.collapse();
			return;
		}
		if (this.calshow) this.calshow.innerHTML = date.toDateString();
		if (this.caldate) this.caldate.innerHTML = date.toDateString();
		this.yearselector.value = date.getFullYear();
		this.dropdown_month.value = date.getMonth()+1;
		this.setDay(date.getDate());
		this.show();
	}
	Calendar.prototype.init = cal_init;
	function cal_set_day(day) {this.day = day; this.draw();}
	Calendar.prototype.setDay = cal_set_day;
	
	function cal_draw() {
		var id=this.id;
		// clear calendar
		for (var i=1; i<=42; i++) {
			_(i + id).innerHTML = '';
			_(i + id).className="ds_spc";
		}
		mth = this.dropdown_month.value;
		year = this.yearselector.value;
		firstofmonth = new Date(mth + '/1/' + year);
		dayoffset = firstofmonth.getDay();
		
		// draw days
		for(var i=1;(new Date(mth + '/' + i + '/' + year)).getMonth()+1 == mth;i++) {
			_((i+dayoffset)+id).innerHTML = 
				"<a href='#' onClick='parentNode.parentNode.parentNode.cal.setDay("+i+");return false;'>"+i+"</a>";
			_((i+dayoffset)+id).className="ds";
		}
		
		if (this.day) {
			while (this.day>27 && (new Date(mth + '/' + this.day + '/' + year).getMonth()+1 != mth)) this.day--;
			// draw selected day
			_((this.day+dayoffset)+id).className="ds_sel";
		}
		this.caldate.innerHTML = new Date(mth + '/' + this.day + '/' + year).toDateString();
	}
	Calendar.prototype.draw = cal_draw
	function cal_save() {
		var datestr = giDateToString(new Date(this.dropdown_month.value+'/'+this.day+'/'+this.yearselector.value));
		if (this.calshow) this.calshow.innerHTML = datestr;
		if (this.ltext) {
			this.ltext.value=datestr;
		} else {
			this.lday.value = this.day;
			this.lmth.value = this.dropdown_month.value;
			this.lyr.value = this.yearselector.value;
		}
	}
	Calendar.prototype.save = cal_save;
	function cal_today() {
		date = new Date();
		this.yearselector.value = date.getFullYear();
		this.dropdown_month.value = date.getMonth()+1;
		this.setDay(date.getDate());
	}										 
	Calendar.prototype.today = cal_today;
